package com.vsked.util;

import java.util.ArrayList;


import com.vsked.bean.*;
import com.vsked.database.*;

public class DaoProcess {
	public static String ConnectionType="ConnectDB.getSqlServerConnection();";
	public static String ConnectionClose="ConnectDB.closeSqlConnection();";
	
	public static String getConnectionDeclare(){
		return "Connection conn = null;";
	}
	public static String getDAOConnectionInfo(){
		return ConnectionType;
	}
	public static String getDAOConnectionClose(){
		return Symbol.getSpaceString(12)+ConnectionClose;
	}
	public static String getImportInfo(){
		return Symbol.enterSymbol+"import java.sql.*;"+Symbol.enterSymbol+"import java.io.*;"+Symbol.enterSymbol+"import java.util.*;"+Symbol.enterSymbol+"import util.*;";		
	}
	public static String getDAO(String inTableName,String inDaoContent){
		return Symbol.enterSymbol+"public class "+Symbol.getStringFirstUpperCase(inTableName)+"DAO {"+inDaoContent+Symbol.enterSymbol+"}";
	}
	public static String getDAODeclareInfo(String inTableName){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"private "+getConnectionDeclare()+Symbol.enterSymbol+Symbol.getSpaceString(4)+"private PreparedStatement pt = null;"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"private ResultSet rs = null;"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"private boolean flag = false;"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"private String sql = \"\";"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"private "+BeanProcess.getObjectDeclareString(inTableName)+"=null;"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"private List<"+Symbol.getStringFirstUpperCase(inTableName)+">"+Symbol.getSpaceString(1)+Symbol.getStringFirstLowerCase(inTableName)+"List=null;"+Symbol.enterSymbol;
	}
	
	public static String getObjectInfo(String inTableName,String inObjectInfoContent){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"public "+Symbol.getStringFirstUpperCase(inTableName)+" get"+Symbol.getStringFirstUpperCase(inTableName)+"(ResultSet rs) throws Exception{"+BeanProcess.getNewObject(inTableName)+inObjectInfoContent+Symbol.enterSymbol+Symbol.getSpaceString(8)+"return "+Symbol.getStringFirstLowerCase(inTableName)+";"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}";	
	}
	public String getAddObjectMethod(String inTableName,String[] inParameterArray,String inPrimaryKey,String inPrimaryKeyDataType){
		return new DaoProcess().getAddOrEditObjectMethod(1,inTableName,inParameterArray,inPrimaryKey,inPrimaryKeyDataType);
	}
	public String getEditObjectMethod(String inTableName,String[] inParameterArray,String inPrimaryKey,String inPrimaryKeyDataType){
		return new DaoProcess().getAddOrEditObjectMethod(2,inTableName,inParameterArray,inPrimaryKey,inPrimaryKeyDataType);
	}
	public String getAddOrEditObjectMethod(int addOrEdit,String inTableName,String[] inParameterArray,String inPrimaryKey,String inPrimaryKeyDataType){
		return addOrEdit==1?Symbol.enterSymbol+Symbol.getSpaceString(4)+"public"+Symbol.getSpaceString(1)+"boolean"+Symbol.getSpaceString(1)+"add("+BeanProcess.getInObjectDeclareString(inTableName)+"){"+ExceptionProcess.getTryCatchFinallyBlock(new DaoProcess().getAddAndEditFramework(1,inTableName,inParameterArray,inPrimaryKey,inPrimaryKeyDataType))+Symbol.enterSymbol+Symbol.getSpaceString(8)+"return flag;"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}":Symbol.enterSymbol+Symbol.getSpaceString(4)+"public"+Symbol.getSpaceString(1)+"boolean"+Symbol.getSpaceString(1)+"edit("+BeanProcess.getInObjectDeclareString(inTableName)+"){"+ExceptionProcess.getTryCatchFinallyBlock(new DaoProcess().getAddAndEditFramework(2,inTableName,inParameterArray,inPrimaryKey,inPrimaryKeyDataType))+Symbol.enterSymbol+Symbol.getSpaceString(8)+"return flag;"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}";
	}
	public String getAddAndEditFramework(int addOrEdit,String inTableName,String[] inParameterArray,String inPrimaryKey,String inPrimaryKeyDataType){
		return addOrEdit==1?Symbol.enterSymbol+Symbol.getSpaceString(8)+"conn="+getDAOConnectionInfo()+Symbol.enterSymbol+Symbol.getSpaceString(8)+"sql="+new Symbol().getAddSql(inParameterArray, inTableName)+";"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"pt = conn.prepareStatement(sql);"+Symbol.enterSymbol+Symbol.getSpaceString(8)+Symbol.getDAOParameterRegString(inParameterArray, inTableName)+Symbol.enterSymbol+Symbol.getSpaceString(8)+"if (pt.executeUpdate() > 0) flag=true;":Symbol.enterSymbol+Symbol.getSpaceString(8)+"conn="+getDAOConnectionInfo()+Symbol.enterSymbol+Symbol.getSpaceString(8)+"sql="+new Symbol().getEditSql(inParameterArray, inTableName,inPrimaryKey,inPrimaryKeyDataType)+";"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"pt = conn.prepareStatement(sql);"+Symbol.enterSymbol+Symbol.getSpaceString(8)+Symbol.getDAOParameterRegString(inParameterArray, inTableName)+Symbol.enterSymbol+Symbol.getSpaceString(8)+Symbol.forDaoEditMethodPrimaryKey+"if (pt.executeUpdate() > 0) flag=true;";
	}
	public static String getFindAllMethod(String inTableName){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"public List<"+Symbol.getStringFirstUpperCase(inTableName)+"> get"+Symbol.getStringFirstUpperCase(inTableName)+"All() {"+Symbol.enterSymbol+Symbol.getSpaceString(8)+Symbol.getStringFirstLowerCase(inTableName)+"List=new ArrayList<"+Symbol.getStringFirstUpperCase(inTableName)+">();"+ExceptionProcess.getTryCatchFinallyBlock("conn = "+getDAOConnectionInfo()+Symbol.enterSymbol+Symbol.getSpaceString(12)+"sql = \"select * from "+inTableName+"\";"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"pt = conn.prepareStatement(sql);"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"rs = pt.executeQuery();"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"while (rs.next()) {"+Symbol.enterSymbol+Symbol.getSpaceString(16)+Symbol.getStringFirstLowerCase(inTableName)+"List.add(get"+Symbol.getStringFirstUpperCase(inTableName)+"(rs));"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"}")+Symbol.enterSymbol+Symbol.getSpaceString(8)+"return "+Symbol.getStringFirstLowerCase(inTableName)+"List;"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}";
	}
	public static String getFindAllMethod(String inTableName,String inPrimarykey){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"public List<"+Symbol.getStringFirstUpperCase(inTableName)+"> get"+Symbol.getStringFirstUpperCase(inTableName)+"All(VskPage vp) {"+Symbol.enterSymbol+Symbol.getSpaceString(8)+Symbol.getStringFirstLowerCase(inTableName)+"List=new ArrayList<"+Symbol.getStringFirstUpperCase(inTableName)+">();"+ExceptionProcess.getTryCatchFinallyBlock("conn = "+getDAOConnectionInfo()+Symbol.enterSymbol+Symbol.getSpaceString(12)+"sql = \"select * from "+inTableName+"\";"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"vp.setAllCountSql(sql);"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"vp.getAllRecordCount(conn, vp.getAllCountSql());"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"pt = conn.prepareStatement(vp.processPageSql( \""+inTableName+"\", \""+inPrimarykey+"\"));"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"rs = pt.executeQuery();"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"while (rs.next()) {"+Symbol.enterSymbol+Symbol.getSpaceString(16)+Symbol.getStringFirstLowerCase(inTableName)+"List.add(get"+Symbol.getStringFirstUpperCase(inTableName)+"(rs));"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"}")+Symbol.enterSymbol+Symbol.getSpaceString(8)+"return "+Symbol.getStringFirstLowerCase(inTableName)+"List;"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}";
	}
	public static String getFindByPirmaryKeyMethod(String inTableName,String inPrimaryKey,String inPrimarKeyDataType){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"public "+Symbol.getStringFirstUpperCase(inTableName)+" get"+Symbol.getStringFirstUpperCase(inTableName)+"By"+Symbol.getStringFirstUpperCase(inPrimaryKey)+"("+("int".equals(inPrimarKeyDataType.toLowerCase())?"int":"String")+Symbol.getSpaceString(1)+"in"+Symbol.getStringFirstUpperCase(inPrimaryKey)+") {"+Symbol.enterSymbol+Symbol.getSpaceString(8)+Symbol.getStringFirstLowerCase(inTableName)+"=null;"+ExceptionProcess.getTryCatchFinallyBlock("conn = "+getDAOConnectionInfo()+Symbol.enterSymbol+Symbol.getSpaceString(12)+"sql = \"select * from "+inTableName+" where "+inPrimaryKey+"=? \";"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"pt = conn.prepareStatement(sql);"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"pt.set"+Symbol.getStringFirstUpperCase(inPrimarKeyDataType)+"(1,in"+Symbol.getStringFirstUpperCase(inPrimaryKey)+");"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"rs = pt.executeQuery();"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"while (rs.next()) {"+Symbol.enterSymbol+Symbol.getSpaceString(16)+Symbol.getStringFirstLowerCase(inTableName)+" = get"+Symbol.getStringFirstUpperCase(inTableName)+"(rs);"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"}")+Symbol.enterSymbol+Symbol.getSpaceString(8)+"return "+Symbol.getStringFirstLowerCase(inTableName)+";"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}";
	}
	public static String getDelByPrimaryKeyMethod(String inTableName,String inPrimaryKey,String inPrimarKeyDataType){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"public boolean del("+inPrimarKeyDataType+Symbol.getSpaceString(1)+"in"+Symbol.getStringFirstUpperCase(inPrimaryKey)+"){"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"return UtilDAO.del(\""+inTableName+"\", \""+inPrimaryKey+"\", in"+Symbol.getStringFirstUpperCase(inPrimaryKey)+");"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}";
	}
	public static void ProcessDAOConnection(ConnectionInfo inCi){
		if("MsSqlServer2000".equals(inCi.getDbType())){
			ConnectionType="ConnectDB.getSqlServerConnection();";
			ConnectionClose="ConnectDB.closeSqlConnection();";
			
		}
		if("MySql".equals(inCi.getDbType())){
			ConnectionType="ConnectDB.getMySqlConnection();";
			ConnectionClose="ConnectDB.closeSqlConnection();";
		}
		
	}
	public static void main(String[] args) throws Exception {
		
	}
}
